home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Comms / Digester / sources / digester.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-09  |  12.1 KB  |  486 lines  |  [TEXT/ttxt]

  1. /*     file: digester.c
  2.     purpose:    digest e-mails from info-mac digest into HTML format
  3.     author:        Andre' C. van der Ham
  4.     e-mail:        A.C.vanderHam@ET.TUDelft.NL
  5.     
  6.     for THINK C users:
  7.         project profile:
  8.             segment 1:     digester
  9.                         MacTraps
  10.             segment 2:    ANSI
  11.                         unix
  12.                         
  13.         project type:    size:     100K
  14.                             flags:    0080    32-bit compatibility
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /*
  21. #define    DEBUG    1
  22. */
  23.  
  24. #define    MUL    "multipart/digest"
  25. #define    BOD    "Info-Mac Digest      "
  26. #define    EOD    "End of Info-Mac Digest"
  27. #define    BOT    "Today's Topics:"
  28. #define    EOT    "--------------------------------------------------------------------"
  29. #define    EOM    "------------------------------\n"
  30. #define    BOF    "[Archived as "
  31. #define    FTP    "<a href=ftp://sumex-aim.stanford.edu%s>SUMEX</a>"
  32. #define    FUN    ", \n<a href=ftp://src.doc.ic.ac.uk/computing/systems/mac%s>UK</a>"
  33. #define    USA    ", \n<a href=ftp://ftp.hawaii.edu/mirrors%s>Hawaii</a>"
  34. #define    JAP    ", \n<a href=ftp://ftp.u-tokyo.ac.jp/pub%s>U-Tokyo</a>"
  35. #define    CAN    ", \n<a href=ftp://ftp.ucs.ubc.ca/pub/mac%s>Canada</a>"
  36. #define    NED    ", \n<a href=ftp://ftp.fenk.wau.nl/pub/mac%s>The Netherlands</a>"
  37. #define    SWD    ", \n<a href=ftp://ftp.sunet.se/mac%s>Sweden</a>"
  38. #define    MOR    10     /* maximum items overrun */
  39. #define    DRE    "http://dutera.et.tudelft.nl/people/vdham/vdham.html"
  40.  
  41. FILE    *outP1, *inP, *outP2, *outP3, *outP;
  42.  
  43. int ParseOne(int TOCiconOn, int buttonsOn, int split)
  44. {
  45.     char    line[512], tempLine[512], whichFile[512], topic[80];
  46.     int    num, vol, issue, i, l;
  47.     int    tocItem = 0;
  48.     int    msgItem = 0;
  49.     int    found, didFtps=0;
  50.     char    name[256];
  51.     char    *cptr;
  52.     
  53.     for(i=0; i<512; i++) whichFile[i]='m';
  54.     
  55.     /* find issue */
  56.  
  57.     printf("Parsing...\n");
  58.             
  59.     found = 0;
  60.     
  61.     do {
  62.             if( fgets( line, 256, inP ) != NULL )
  63.             {
  64.                 if( strstr( line, BOD ) != NULL )
  65.                 {
  66.                     printf("Found %s\n", BOD);
  67.                     printf(line);
  68.                     found = 1;
  69.                     cptr = (char *)strstr( line, "Volume" );
  70.                     if( cptr==NULL )
  71.                     {
  72.                         printf("Can't find volume number\n");
  73.                         return(0);
  74.                     }
  75.                     vol = atoi( cptr+7 );                
  76.                     cptr = (char *)strstr( line, "Issue" );
  77.                     if( cptr==NULL )
  78.                     {
  79.                         printf("Can't find issue number\n");
  80.                         return(0);
  81.                     }
  82.                     issue = atoi( cptr+6 );                
  83.                     if( !split )
  84.                     {
  85.                         sprintf(name, "IM%d-%d.html", issue, vol);
  86.                         printf("name = '%s'\n", name);
  87.                         if( (outP1 = fopen( name,"w")) == NULL )
  88.                         {
  89.                             printf("Can't open '%s'\n", name );
  90.                             return(0);
  91.                         }
  92.                     }
  93.                     else /* split files */
  94.                     {
  95.                         sprintf(name, "IMt%d-%d.html", issue, vol);
  96.                         printf("name = '%s'\n", name);
  97.                         if( (outP1 = fopen( name,"w")) == NULL )
  98.                         {
  99.                             printf("Can't open '%s'\n", name );
  100.                             return(0);
  101.                         }
  102.  
  103.                         sprintf(name, "IMp%d-%d.html", issue, vol);
  104.                         printf("name = '%s'\n", name);
  105.                         if( (outP2 = fopen( name,"w")) == NULL )
  106.                         {
  107.                             printf("Can't open '%s'\n", name );
  108.                             return(0);
  109.                         }
  110.  
  111.                         sprintf(name, "IMm%d-%d.html", issue, vol);
  112.                         printf("name = '%s'\n", name);
  113.                         if( (outP3 = fopen( name,"w")) == NULL )
  114.                         {
  115.                             printf("Can't open '%s'\n", name );
  116.                             return(0);
  117.                         }
  118.  
  119.                     }
  120.                 }
  121.             }
  122.             else
  123.                 return(0);
  124.     } while( !found );
  125.  
  126. #ifdef DEBUG
  127.     printf("Title found\n");
  128. #endif
  129.  
  130.     fprintf(outP1,"<TITLE>Volume %d : Issue %d</TITLE>\n", vol, issue);
  131.     fprintf(outP1,"<pre>\n");
  132.     fprintf(outP1,"<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a> %s",
  133.                         issue, vol, line);
  134.     fprintf(outP1,"<hr>\n\n");
  135.  
  136.     if( split )
  137.     {
  138.         fprintf(outP2,"<TITLE>Volume %d : Issue %d</TITLE>\n", vol, issue);
  139.         fprintf(outP2,"<pre>\n");
  140.         fprintf(outP2,"<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a> %s",
  141.                             issue, vol, line);    
  142.         fprintf(outP2,"<hr>\n\n");
  143.  
  144.         fprintf(outP3,"<TITLE>Volume %d : Issue %d</TITLE>\n", vol, issue);
  145.         fprintf(outP3,"<pre>\n");
  146.         fprintf(outP3,"<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a> %s",
  147.                             issue, vol, line);    
  148.         fprintf(outP3,"<hr>\n\n");
  149.     }
  150.     
  151.     /* find toc */
  152.     
  153.     found = 0;
  154.     
  155.     do {
  156.             if( fgets( line, 256, inP ) != NULL )
  157.             {
  158.                 if( strstr( line, BOT ) != NULL )
  159.                 {
  160.                     found = 1;
  161.                 }
  162.             }
  163.             else
  164.             {
  165.                 fclose( outP1 );
  166.                 if(split)
  167.                 {
  168.                     fclose(outP2);
  169.                     fclose(outP3);
  170.                 }
  171.                 return(0);
  172.             }
  173.     } while( !found );
  174.  
  175.  
  176. #ifdef DEBUG
  177.     printf("TOC found\n");
  178. #endif
  179.  
  180.     line[strlen(line)-1] = 0;
  181.     fprintf(outP1, "<a name=toc>%s</a>\n", line);
  182.  
  183.     fgets( line, 256, inP ); /* get empty line */
  184.     fputs("<ul>\n", outP1);
  185.         
  186.     while( strlen(fgets( line, 256, inP ))>1 )
  187.     {
  188.         i=0;
  189.         while( i<strlen(line) && line[i]==' ' ) i++;
  190.         if( i==strlen(line) ) i=0;
  191.         
  192.         tocItem++;
  193.         line[strlen(line)-1]=0;    /* terminate line with 0 */
  194.         
  195.         if( strstr( (line+i), "[*]" )!=NULL )
  196.         {
  197.             whichFile[tocItem] = 'p';
  198.             strcpy(topic, "prog_topic.gif");
  199.         }
  200.         else
  201.             strcpy(topic, "msg_topic.gif");
  202.         
  203.         if( TOCiconOn )
  204.         {
  205.             if(split)
  206.             {
  207.                 fprintf(outP1, "<img src=%s alt=*><a name=toc%d> </a><a href=IM%c%d-%d.html#item%d>%s</a>\n",
  208.                         topic, tocItem, whichFile[tocItem], issue, vol, tocItem, line+i);
  209.             }
  210.             else
  211.             {
  212.                 fprintf(outP1, "<img src=%s alt=*><a name=toc%d> </a><a href=#item%d>%s</a>\n",
  213.                         topic, tocItem, tocItem, line+i);
  214.             }
  215.         }
  216.         else
  217.         {
  218.             if(split)
  219.             {
  220.                 fprintf(outP1, "<li><a name=toc%d> </a><a href=IM%c%d-%d.html#item%d>%s</a>\n",
  221.                         tocItem, whichFile[tocItem], issue, vol, tocItem, line+i);
  222.             }
  223.             else
  224.             {
  225.                 fprintf(outP1, "<li><a name=toc%d> </a><a href=#item%d>%s</a>\n",
  226.                         tocItem, tocItem, line+i);
  227.             }
  228.         }
  229.     }    
  230.  
  231.     fprintf(outP1, "</ul>\n\n\n");
  232.         
  233.     do
  234.     {
  235.         fgets(line, 256, inP);
  236.         fputs( line, outP1 );
  237.     }
  238.     while( (strstr( line, EOT ) == NULL) && strcmp( line, EOM ) );
  239.     
  240.     /* do messages */
  241.     
  242.     found = 0;
  243.     
  244.     do{
  245.         msgItem++;
  246.         
  247.         if(!split)                                     outP=outP1;
  248.         else if( whichFile[msgItem]=='p' )    outP=outP2;
  249.         else                                            outP=outP3;
  250.         
  251.         if( (whichFile[msgItem]=='p')&&(didFtps==0) )
  252.         {
  253.             fputs("\n</pre>", outP);
  254.             fprintf(outP, "Make FTP connection with: ");
  255.             strcpy( name, "/info-mac/");
  256.             
  257.             fprintf( outP, FTP, name);            
  258.             fprintf( outP, FUN, name);            
  259.             fprintf( outP, USA, name);            
  260.             fprintf( outP, JAP, name);            
  261.             fprintf( outP, CAN, name);            
  262.             fprintf( outP, NED, name);            
  263.             fprintf( outP, SWD, name);            
  264.         
  265.             fprintf( outP, ".\n<p><hr><p>\n<pre>\n" );
  266.             
  267.             didFtps=1;
  268.         }
  269.  
  270.         fprintf( outP, "<a name=item%d> </a>",msgItem);
  271.  
  272.         if( buttonsOn )
  273.         {
  274.             if(split && (whichFile[msgItem+1]!=whichFile[msgItem]) )
  275.                             fprintf( outP, "<a href=IM%c%d-%d.html#item%d><img src=next.gif alt=Next></a> ", whichFile[msgItem+1], issue, vol, msgItem+1);
  276.             else            fprintf( outP, "<a href=#item%d><img src=next.gif alt=Next></a> ", msgItem+1);
  277.     
  278.             if( msgItem>tocItem )
  279.                 if(split)    fprintf( outP, "<a href=IMt%d-%d.html#toc><img src=toc.gif alt=TOC></a> ", issue, vol);
  280.                 else            fprintf( outP, "<a href=#toc><img src=toc.gif alt=TOC></a> ");
  281.             else
  282.                 if(split)    fprintf( outP, "<a href=IMt%d-%d.html#toc%d><img src=toc.gif alt=TOC></a> ", issue, vol, msgItem);
  283.                 else            fprintf( outP, "<a href=#toc%d><img src=toc.gif alt=TOC></a> ", msgItem);
  284.             
  285.             fprintf( outP, "<a href=index.html#issue%d-%d><img src=digester.gif alt=Index></a>\n\n", 
  286.                                 issue, vol);
  287.         }
  288.         else
  289.         {
  290.             if(split && (whichFile[msgItem+1]!=whichFile[msgItem]) )    
  291.                             fprintf( outP, "<a href=IM%c%d-%d.html#item%d>Next</a> ", whichFile[msgItem+1], issue, vol, msgItem+1);
  292.             else            fprintf( outP, "<a href=#item%d>Next</a> ", msgItem+1);
  293.  
  294.             if( msgItem>tocItem )
  295.                 if(split)    fprintf( outP, "<a href=IMt%d-%d.html#toc>TOC</a> ", issue, vol);
  296.                 else            fprintf( outP, "<a href=#toc>TOC</a> ");
  297.             else
  298.                 if(split)    fprintf( outP, "<a href=IMt%d-%d.html#toc%d>TOC</a> ", issue, vol, msgItem);
  299.                 else            fprintf( outP, "<a href=#toc%d>TOC</a> ", msgItem);
  300.  
  301.             fprintf( outP, "<a href=index.html#issue%d-%d>INDEX</a>\n\n", issue, vol );
  302.         }
  303.         
  304.         while( strcmp( fgets( line, 256, inP ), EOM )!=0 &&
  305.                  strstr( line, EOD ) == NULL && !feof(inP) )
  306.         {
  307.             /* filter entities in mail */
  308.             
  309.             tempLine[0]=0;
  310.             l = 0;
  311.             for(i=0; i<strlen(line); i++ )
  312.             {
  313.                 if( line[i]=='<' )
  314.                 {
  315.                     tempLine[l++] = '&';
  316.                     tempLine[l++] = 'l';
  317.                     tempLine[l++] = 't';
  318.                     tempLine[l++] = ' ';
  319.                 }
  320.                 else
  321.                 if( line[i]=='>' )
  322.                 {
  323.                     tempLine[l++] = '&';
  324.                     tempLine[l++] = 'g';
  325.                     tempLine[l++] = 't';
  326.                     tempLine[l++] = ' ';
  327.                 }
  328.                 else
  329.                 if( line[i]=='"' )
  330.                 {
  331.                     tempLine[l++] = '&';
  332.                     tempLine[l++] = 'q';
  333.                     tempLine[l++] = 'u';
  334.                     tempLine[l++] = 'o';
  335.                     tempLine[l++] = 't';
  336.                     tempLine[l++] = ' ';
  337.                 }
  338.                 else
  339.                 if( line[i]=='&' )
  340.                 {
  341.                     tempLine[l++] = '&';
  342.                     tempLine[l++] = 'a';
  343.                     tempLine[l++] = 'm';
  344.                     tempLine[l++] = 'p';
  345.                     tempLine[l++] = ' ';
  346.                 }
  347.                 else
  348.                 {
  349.                     tempLine[l++] = line[i];
  350.                     tempLine[l] = 0;
  351.                 }
  352.             }
  353.             strcpy( line, tempLine );
  354.             
  355.             fputs(line, outP);
  356.             
  357.             if( (cptr=strstr( line, "/info-mac/" )) != NULL )
  358.             {
  359.                 num = cptr-line;
  360.                 i = 0;
  361.                 while( line[num] != ';' && num<strlen(line) && i<256 )
  362.                 {
  363.                     name[i]=line[num];
  364.                     i++;
  365.                     num++;
  366.                 }
  367.                 name[i] = 0;
  368.                 line[strlen(line)-1]=0;
  369.                 
  370.                 fprintf( outP, "\n</pre>\nFetch from " );
  371.                 
  372.                 fprintf( outP, FTP, name);            
  373.                 fprintf( outP, FUN, name);            
  374.                 fprintf( outP, USA, name);            
  375.                 fprintf( outP, JAP, name);            
  376.                 fprintf( outP, CAN, name);            
  377.                 fprintf( outP, NED, name);            
  378.                 fprintf( outP, SWD, name);            
  379.                 
  380.                 fprintf( outP, ".\n<pre>\n" );
  381.             }
  382.         }
  383.         fputs(line, outP);
  384.                     
  385.     }while( strstr( line, EOD ) == NULL && msgItem<(tocItem+MOR) && !feof( inP ) );
  386.     
  387.     fprintf( outP1, "</pre>\n\n\n" );
  388.     if(!split) fputs("<p><hr><p>\n", outP1);
  389.     fprintf( outP1, "<a name=item%d> </a>",msgItem+1);
  390.     fputs("HTML file created by digester<p>\n", outP1);
  391.     fprintf(outP1,"Digester developed by: <a href=%s>André  C. van der Ham</a><p>\n", DRE);
  392.     fputs("<address>\n", outP1);
  393.     fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP1);
  394.     fputs("</address>\n", outP1);    
  395.  
  396.     fclose( outP1 );
  397.  
  398.     if(split)
  399.     {
  400.         fprintf( outP2, "</pre>\n\n\n" );
  401.         fprintf( outP2, "<a name=item%d> </a>",msgItem+1);
  402.         fputs("HTML file created by digester<p>\n", outP2);
  403.         fprintf(outP2,"Digester developed by: <a href=%s>André  C. van der Ham</a><p>\n", DRE);
  404.         fputs("<address>\n", outP2);
  405.         fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP2);
  406.         fputs("</address>\n", outP2);    
  407.  
  408.         fclose( outP2 );
  409.  
  410.         fprintf( outP3, "</pre>\n\n\n" );
  411.         fputs("<p><hr><p>\n", outP3);
  412.         fprintf( outP3, "<a name=item%d> </a>",msgItem+1);
  413.         fputs("HTML file created by digester<p>\n", outP3);
  414.         fprintf(outP3,"Digester developed by: <a href=%s>André  C. van der Ham</a><p>\n", DRE);
  415.         fputs("<address>\n", outP3);
  416.         fputs("Send bug reports, questions, etc. to: A.C.vanderHam@ET.TUDelft.NL<p>\n", outP3);
  417.         fputs("</address>\n", outP3);    
  418.  
  419.         fclose( outP3 );
  420.     }
  421.     
  422.     printf("file closed.\n");
  423.      
  424.     if( feof( inP ) )
  425.         printf("Premature end of file found on input file.\n");
  426.         
  427.     if( msgItem != tocItem )
  428.         printf("!! msgItem = %d, tocItem = %d\n", msgItem, tocItem );
  429.         
  430.     return(1);
  431. }
  432.  
  433. int main( void )
  434. {
  435.     char    name[80], answer[80];
  436.     char    line[256];
  437.     int    ButtonsOn=0, TOCiconOn=0, split=0;
  438.  
  439.     int    okay=1;
  440.  
  441.     puts("\n\n*** D I G E S T E R ***\n");
  442.     puts("by Andre' C. van der Ham\n");
  443.     puts("This program parses info-mac digests you get by e-mail.");
  444.     puts("Save the e-mail without headers and paragraph recognition (Eudora)");
  445.     puts("Convert file to appropriate ascii format, for example: Mac->Unix\n");
  446.     puts("The file can contain a number of digests.");
  447.     puts("An HTML file is generated for each issue.");
  448.     puts("Send bug reports, questions, etc... to:");
  449.     puts("e-mail: A.C.vanderHam@ET.TUDelft.NL\n\n");
  450.  
  451.     printf("Input file (infomac.txt): ");        
  452.     gets( name );
  453.     if(strlen(name)<2) strcpy( name, "infomac.txt");
  454.  
  455.     printf("Use fancy topics ICON {gif: topic.gif} (y/N): ");
  456.     gets( answer );
  457.     
  458.     if( answer[0]=='y' ) TOCiconOn=1;
  459.     
  460.     printf("Use fancy buttons {gifs: next.gif and toc.gif} (y/N): ");
  461.     gets( answer );
  462.     
  463.     if( answer[0]=='y' ) ButtonsOn=1;
  464.     
  465.     printf("Split file into 3 separate files for topics, programs and messages (y/N): ");
  466.     gets( answer );
  467.     
  468.     if( answer[0]=='y' ) split=1;
  469.     
  470.     if( (inP = fopen( name, "r" )) != NULL )
  471.     {
  472.         while( okay )
  473.         {
  474.             okay = ParseOne(TOCiconOn, ButtonsOn, split);
  475.             
  476. #ifdef DEBUG
  477.             printf("%d: next...\n", okay);
  478. #endif
  479.  
  480.         }
  481.  
  482.         fclose( inP );
  483.     }
  484.     
  485.     return(0);
  486. }